/**
 * UTSDK.AJAX
 * Used for ajax calls to the database
 * 
 * VERSIONING:
 * v1.0 - Created AJAX class
 * v1.1 - Update x method
 * v1.2 - Updated post to be able to take json and forms
 * v1.3 - Added timestamp to get requests to fix IE request caching
 */
var ajax = UTSDK.AJAX;
/**
 * Returns a XMLHttpRequest used for ajax calls
 */
ajax.x = function() {
    if (typeof XMLHttpRequest !== 'undefined') {
        return new XMLHttpRequest();
    }
    var versions = [
        "MSXML2.XmlHttp.5.0",
        "MSXML2.XmlHttp.4.0",
        "MSXML2.XmlHttp.3.0",
        "MSXML2.XmlHttp.2.0",
        "Microsoft.XmlHttp"
    ];

    var xhr;
    for (var i = 0; i < versions.length; i++) {
        try {
            xhr = new ActiveXObject(versions[i]);
            break;
        }
        catch (e) {
        }
    }
    return xhr;
};
/**
 * This method creates the correct parameters to send an ajax request.  Converts
 * jcon url parameters into a valid GET request url and passes on the callback
 * method
 * @param {string} url of the page to handle the request
 * @param {json} data json object of the values to be added to the request
 * @param {method} callback method to be called when the ajax call is complete
 * @param {boolean} sync whether ajax is async call or not
 */
ajax.get = function(url, data, callback, sync) {
    data["timestamp"] = UTSDK.getTimestamp();
    url += "?" + ajax.jsonToURL(data);
    ajax.send("GET", url, callback, null, sync, data);
};
/**
 * This method creates the correct parameters to send an ajax request.  Converts
 * jcon url parameters into a valid POST request url and passes on the callback
 * method
 * @param {string} url of the page to handle the request
 * @param {json} data json object of the values to be added to the request
 * @param {method} callback method to be called when the ajax call is complete
 * @param {boolean} sync whether ajax is async call or not
 */
ajax.post = function(url, data, callback, sync) {
    if (data.innerHTML)
        ajax.send("POST", url, callback, ajax.formToURL(data), sync, data);
    else
        ajax.send("POST", url, callback, ajax.jsonToURL(data), sync, data);
};
/**
 * This method handles the sending of the ajax request.  It retrieves a XMLHttpRequest
 * then prepares the request to be sent.
 * @param {string} method request method POST|GET
 * @param {string} url of the request
 * @param {method} callback the method to call after the request is complete
 * @param {string} data the data to send for a post request
 * @param {boolean} sync whether the request is async or not
 * @param {json} original object parsed 
 */
ajax.send = function(method, url, callback, data, sync, original) {
    var x = ajax.x();
    x.open(method, url, sync);
    x.onreadystatechange = function() {
        if (x.readyState === 4 && x.status === 200) {
            if (callback)
                callback(x.responseText, x.original);
        }
    };
    if (method === 'POST') {
        x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    }
    if (original)
        x.original = original;
    x.send(data);
};
/**
 * Converts a json object to a url request friendly string of the form
 * key1=value1&key2=value2
 * @param {json} json the json to convert
 * @returns {String} a url parameter friendy string
 */
ajax.jsonToURL = function(json) {
    var array = [];
    for (var key in json) {
        array.push(key + "=" + json[key]);
    }
    return array.join("&");
};
/**
 * Converts a form element to a url request friendly string of the form
 * key1=value1&key2=value2
 * @param {form} form to convert
 * @returns {String} a url parameter friendy string
 */
ajax.formToURL = function(form) {
    var array = [];
    for (var i = 0; i < form.elements.length; i++) {
        if (form.elements[i].name)
            array.push(form.elements[i].name + "=" + form.elements[i].value);
    }
    return array.join("&");
};